package cn.edu.lingnan.translation.controller;


import cn.edu.lingnan.translation.entity.User;
import cn.edu.lingnan.translation.entity.vo.UserQueryVo;
import cn.edu.lingnan.translation.service.UserService;
import cn.edu.lingnan.translation.utils.MD5;
import cn.edu.lingnan.translation.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bao
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @Description 获取用户分页列表
     * @Param [page, limit, userQueryVo]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/12 14:36
     */
    @ApiOperation(value = "获取用户分页列表")
    @PostMapping("/{page}/{limit}")
    public R getUserPage(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
            @RequestBody UserQueryVo userQueryVo) {
        // 分页
        Page<User> userPage = new Page<>(page, limit);
        // 条件查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(userQueryVo.getUsername())) {
            wrapper.like("username", userQueryVo.getUsername());
        }
        if(!StringUtils.isEmpty(userQueryVo.getMobile())) {
            wrapper.like("mobile", userQueryVo.getMobile());
        }
        if(!StringUtils.isEmpty(userQueryVo.getNickname())) {
            wrapper.like("nickname", userQueryVo.getNickname());
        }
        IPage<User> iPage = userService.page(userPage, wrapper);
        return R.ok().data("users", iPage.getRecords()).data("total", iPage.getTotal());
    }

    /**
     * @Description 通过id获取用户信息
     * @Param [id]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/14 9:09
     */
    @ApiOperation(value = "通过id获取用户信息")
    @GetMapping("/{id}")
    public R getUserById(@PathVariable String id) {
        User user = userService.getById(id);
        return R.ok().data("user", user);
    }


    /**
     * @Description 用户注册
     * @Param [user]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/12 11:06
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/add")
    public R addUser(@RequestBody User user) {
        user.setPassword(MD5.encrypt(user.getPassword()));
        if (StringUtils.isEmpty(user.getSign())) {
            user.setSign("这个人很懒，什么都没留下");
        }
        userService.save(user);
        return R.ok();
    }

    /**
     * @Description 用户删除
     * @Param [id]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/13 16:23
     */
    @ApiOperation(value = "用户删除")
    @DeleteMapping("/delete/{id}")
    public R deleteUser(@PathVariable String id) {
        userService.removeById(id);
        return R.ok();
    }

    /**
     * @Description 用户更新
     * @Param [user]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/13 16:24
     */
    @ApiOperation(value = "用户更新")
    @PostMapping("/save")
    public R saveUser(@RequestBody User user) {
        userService.updateById(user);
        return R.ok();
    }
}

